package se.sudoku;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;


public class SudokuScoreBoard extends Activity {

	//SUDOKUMENU ITEMS
	private ImageView winfailImage;
	private PopupWindow scoreboard;
	private Button scoreboard_btn_1, scoreboard_btn_2;
	private Activity activity;

	private final int WIN = 0;
	private final int LOSE = 1;
	
	//constructor
	public SudokuScoreBoard(Context c) {
		activity = (Activity) c;
		//popup window
		scoreboard = new PopupWindow(View.inflate(c, R.layout.sudokuscorescreen, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);	    
		scoreboard.setAnimationStyle(R.style.anim_fade);
		//buttons here
		scoreboard_btn_1 = (Button)scoreboard.getContentView().findViewById(R.id.sudokuscoreboard_btn1);
		scoreboard_btn_1.setOnClickListener(sudukoScoreBoardListener);
	    scoreboard_btn_2 = (Button)scoreboard.getContentView().findViewById(R.id.sudokuscoreboard_btn2);
	    scoreboard_btn_2.setOnClickListener(sudukoScoreBoardListener);
	    
	    winfailImage = (ImageView)scoreboard.getContentView().findViewById(R.id.winfail);
	  
	}
	
	//toggle windows
	public void toggleScoreBoardWindow(int check){  
		
		if(check == WIN)
		{
			winfailImage.setImageResource(R.drawable.winner2);
			//winfailImage.setVisibility(View.VISIBLE);
		}
		else if(check == LOSE)
		{
			 
			winfailImage.setImageResource(R.drawable.lose2);
			//winfailImage.setVisibility(View.VISIBLE);
		}
		
    	if(!scoreboard.isShowing()){
    		scoreboard.showAtLocation(activity.findViewById(R.id.sudokuview), Gravity.CENTER, 0, 0);
    	}
    	else{
    		scoreboard.dismiss();
    	}
    }
	
	
	//SUDOKU MENU CLICK LISTENERS
	private OnClickListener sudukoScoreBoardListener = new OnClickListener() {
		public void onClick(View v) {
			
			//resume
			if(v==scoreboard_btn_1){
				toggleScoreBoardWindow(3);
			}
			//main menu
			else if(v==scoreboard_btn_2){
				//quit game
				toggleScoreBoardWindow(3);
				activity.setResult(RESULT_CANCELED);
				activity.finish();
			}
	    }
	};
}